package cn.lili;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.jfinal.kit.Ret;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Arrays;
import java.util.Date;

@Slf4j
public class TestAlipay {
    private static AlipayClient alipayClient;

    static {

        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl("https://openapi.alipay.com/gateway.do");
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset("utf-8");
        certAlipayRequest.setSignType("RSA2");
        certAlipayRequest.setAppId("2021003143600908");
        certAlipayRequest.setPrivateKey("MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCRr82YFByNmDau/d4xK4M9r8ho+JCrAGv+KlqLIT8z27oAPvHHT/jPlYMZB/b7ZM/AFiFC3K/Ot5/4klN5f8s/wj+CzcSPUlNpv/1WJ+UZp2oIKbYI7+mlsrSS4hhcfSUuHbmwRFHU4dd4kwdjfDcihHWlpAHZShsMYDOADJWBJMBS2E6/3C494SjyWwkvSmmrBiDmkfgdDU5yEX88en+1rTyIIPaORMO0laXgb9rwtRSWvvrtYE21mYalAWKcoErYwCIovERsniwYFXsNxWaJRf+yc7ltcOHlaZgGcKd5LK1scKsqE2p6fGb9GA1InRBeU7wphidfeooTsBCzjVRbAgMBAAECggEATlAfh1mAQ8kH1CdZcjr+sIE2jN7Y/WMbqiPgShvUGq9Jm+o6E7b0e7nWH/V9GYQGfS4oMQXqZ4ejsX4O6yf7/MkRVOIBgD1z40u2BcMqTxEEWWD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yxAWj3uTHYydja4zShSXVTM9icNYQ==");
        certAlipayRequest.setCertPath("D:\\source\\lilishop\\cert\\alipay\\appCertPublicKey_2021003143600908.crt");
        certAlipayRequest.setAlipayPublicCertPath("D:\\source\\lilishop\\cert\\alipay\\alipayCertPublicKey_RSA2.crt");
        certAlipayRequest.setRootCertPath("D:\\source\\lilishop\\cert\\alipay\\alipayRootCert.crt");
        try {
            alipayClient = new DefaultAlipayClient(certAlipayRequest);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
    }

    @SneakyThrows
    public static void main(String[] args) {
        wapPay();
    }

    public static void pcPay() throws AlipayApiException {
        String outTradeNo = System.currentTimeMillis() + "";
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setTimeoutExpress("5m");

        model.setOutTradeNo(outTradeNo + "1");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        model.setTotalAmount("1.10");
        model.setSubject("在线订单支付1");


        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        request.setNotifyUrl("https://buyer.hnguanpei.cn/buyer/payment/so/notify/ALIPAY");
        request.setBizModel(model);
        AlipayTradePagePayResponse response = alipayClient.certificateExecute(request);
//        log.info("支付宝合单结果：{}", JSON.toJSONString(response, SerializerFeature.PrettyFormat));
        if (response.isSuccess()) {
            System.out.println(response.getBody());
        }
    }

    public static void wapPay() throws AlipayApiException {
//        String outTradeNo = System.currentTimeMillis() + "";
//        AlipayTradeWapPayModel model = new AlipayTradeWapPayModel();
//        model.setTimeoutExpress("5m");
//
//        model.setOutTradeNo(outTradeNo + "1");
//        model.setProductCode("QUICK_WAP_WAY");
//        model.setTotalAmount("1.10");
//        model.setSubject("在线订单支付1");
//
//
//        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
//        request.setNotifyUrl("https://buyer.hnguanpei.cn/buyer/payment/so/notify/ALIPAY");
//        request.setBizModel(model);
//        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
////        log.info("支付宝合单结果：{}", JSON.toJSONString(response, SerializerFeature.PrettyFormat));
//        if (response.isSuccess()) {
//            System.out.println(response.getBody());
//        }
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipay.com/gateway.do",
                "2021003143600908",
                "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQD4N0ctvp6BUmDFLSViAB1RrMru1zhZ3ZI/IZwzo8MN4oPSZHW9lDdJS9tsRk3BWmYxfCGo1UY6oRjqje9d22gd4OEAZLji1bfl8dF3DrjLpYgZWin1FX/I32E/5NtMoKWl0Dzh2jURVMQLKYJ0R3LCJTnGJtkutCcVCa9OWZN3C7HdhkAvQcoJpSFHoqHyZxTYvwnVwAO69vPGv1BZoOKLikpScE9SdcFNLKEx1ybVArE5jF2Xb52WNGGcs2OPWtutdbsTkpDlYNKeNaJJL+HtP6bd1Y1mf8PvH3r9rKIbn8vqBvIThcq8lRFdzGe4S+lZwDJrziGIGIMIUnj0pUr7AgMBAAECggEASwZiVFhWjp44s8CWmQ2RzxscZ57T4I3RY5WkIhOwpGTAFXrZ/8hqAZSVKSAC5puNYg20GtUEGjqRTmxCWqszyDwtpjHLlHymzjEOcB8aCivpfEg9gF/UNQIX4fkps/qmtvy/OkD7Y1asvvb4AG+MJ1SbJuKdKLm5SmmX/GWk5gkhgPIp5FuVT7wIWAxA6VVl7k5A20nQq/ArU2nQfMZ75337DuXk2T/qcVCdOjZfjgZxC4OPXlbHVd0h8VO1G2eyoipTELMTRN1SpARbW0sBJLP7zotWxIUbRuhlSLLPB+ZnrquBHcRXkWkxWEHJgswrabh3CSsruGOhL0EtifL1CQKBgQD90LsSNi8GiuI0kZL0+T8ETKojEaBHVyAFiLSpws+DtjsvMSSYcoOL5JYHSmWK4VipLyv7N8kk84g2+quVz5I7Rpip8HGTS2N9nD8EdvoZb4kAlTaOvvFRqWqbsxpDIMKypMBqgYjQfXb5WqQFKxmcJ6aQSUhu+8D6Jk2vOd9cxwKBgQD6WjWRN9LCG7Y+6n8qogx08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y50+dwIDAQAB",
                "RSA2"
        );
        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setNotifyUrl("https://buyer.hnguanpei.cn/buyer/payment/so/notify/ALIPAY");
        request.setReturnUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "O" + System.currentTimeMillis());
        bizContent.put("total_amount", 0.01);
        bizContent.put("subject", "测试商品" + DateFormatUtils.format(new Date(), "yyyyMMddHHmmss"));
        bizContent.put("product_code", "QUICK_WAP_WAY");
        bizContent.put("settle_info", Ret.create("settle_detail_infos", Arrays.asList(Ret.create("trans_in_type", "defaultSettle").set("trans_in", "").set("amount", 0.01))));
        bizContent.put("sub_merchant", Ret.create("merchant_id", "2088441813361580"));

        request.setBizContent(bizContent.toString());

        System.out.println(request.getBizContent());
        AlipayTradeWapPayResponse response = alipayClient.pageExecute(request);
        if (response.isSuccess()) {
            System.out.println(response.getBody());
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
        }
    }
}
